#include "changepswd.h"
#include "ui_changepswd.h"

changePswd::changePswd(int index,QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::changePswd)
{
    ui->setupUi(this);

    m_index=index;

}

changePswd::~changePswd()
{
    delete ui;
}

void changePswd::on_pushButton_2_clicked()
{
    this->hide();
    if(m_index==0){
        manger* m=new manger();
        m->show();
    }else if(m_index==1){
        studentForm* s= new studentForm;
        s->show();
    }else if(m_index==2){
        teacherForm* t=new teacherForm;
        t->show();
    }
}


void changePswd::on_pushButton_clicked()
{
    QString name=username_qj;
    QString oldPswd=ui->editOldPswe->text();
    QString newPswd=ui->editNewPswd->text();
    QString rePswd=ui->editRePswd->text();

    if(name.isEmpty()||oldPswd.isEmpty()||newPswd.isEmpty()||rePswd.isEmpty()){
        QMessageBox::warning(this,"警告","请输入完整信息.");
        return;
    }
    if(newPswd!=rePswd){
        QMessageBox::warning(this,"警告","两次密码输入不一致.");
        return;
    }

    QSqlDatabase db;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
        db = QSqlDatabase::database("qt_sql_default_connection");
    else
        db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("studentmanager");
    db.setUserName(sqluser);
    db.setPassword(sqlpass);
    if (!db.open())
        qDebug() << "Failed to connect to root mysql admin";
    else qDebug() << "open";


    QSqlQuery query(db);

    if(m_index==0){
        // where username="+name+";"
        query.exec("select * from admin where username='"+name+"';");
        query.first();
        if(query.isValid()){
            if(oldPswd==query.value("password").toString()){
                if(query.exec("update admin set password='"+newPswd+"' where password='"+oldPswd+"';")){
                    QMessageBox::information(this,"提示","密码修改成功.");
                    ui->pushButton_2->click();
                }else{
                    QMessageBox::warning(this,"警告","密码修改失败.");
                    return;
                }
            }else{
                QMessageBox::warning(this,"警告","旧密码错误.");
                return;
            }
        }else{
            QMessageBox::warning(this,"警告","该账号不存在..");
            return;
        }

    }else if(m_index==1){
        query.exec("select * from student where name='"+name+"';");
        query.first();
        if(query.isValid()){
            if(oldPswd==query.value("password").toString()){
                if(query.exec("update student set password='"+newPswd+"' where password='"+oldPswd+"';")){
                    QMessageBox::information(this,"提示","密码修改成功.");
                    ui->pushButton_2->click();
                }else{
                    QMessageBox::warning(this,"警告","密码修改失败.");
                    return;
                }
            }else{
                QMessageBox::warning(this,"警告","旧密码错误.");
                return;
            }
        }else{
            QMessageBox::warning(this,"警告","该账号不存在..");
            return;
        }
    }else if(m_index==2){
        query.exec("select * from teacher where name='"+name+"';");
        query.first();
        if(query.isValid()){
            if(oldPswd==query.value("pswd").toString()){
                if(query.exec("update teacher set pswd='"+newPswd+"' where pswd='"+oldPswd+"';")){
                    QMessageBox::information(this,"提示","密码修改成功.");
                    ui->pushButton_2->click();
                }else{
                    QMessageBox::warning(this,"警告","密码修改失败.");
                    return;
                }
            }else{
                QMessageBox::warning(this,"警告","旧密码错误.");
                return;
            }
        }else{
            QMessageBox::warning(this,"警告","该账号不存在..");
            return;
        }
    }
}

